package com.gress.shop.dao.impl;

import com.google.common.collect.Lists;
import com.gress.shop.bo.Menu;
import com.gress.shop.bo.example.MenuExample;
import com.gress.shop.dao.IMenuDao;
import com.gress.shop.enums.MenuEnum;
import com.gress.shop.mapper.MenuMapper;
import com.gress.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created xiongwen with 2018/3/6 15:43
 * Description:
 */
@Service
public class MenuDaoImpl implements IMenuDao{

    @Resource
    private MenuMapper menuMapper;

    @Override
    public Menu update(Menu menu) {
        menuMapper.updateByPrimaryKeySelective(menu);
        return menu;
    }

    @Override
    public Menu save(Menu menu) {
        menu.setId(UUIDUtil.getUUID());
        menuMapper.insertSelective(menu);
        return menu;
    }

    @Override
    public int deleteById(String id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Menu selectMenuById(String id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Menu> queryByExample(MenuExample example) {
        return menuMapper.selectByExample(example);
    }

    @Override
    public List<Menu> queryBLawfulMenu() {
        MenuExample example = new MenuExample();
        example.createCriteria()
                .andIsValidEqualTo(1);
        List<Menu> menuList = menuMapper.selectByExample(example);
        return menuList;
    }

    @Override
    public List<Menu> queryIsNotMenu() {
        MenuExample example = new MenuExample();
        example.createCriteria()
                .andIsValidEqualTo(1).andTypeNotEqualTo(MenuEnum.MENU.getMenuCode());
        List<Menu> menuList = menuMapper.selectByExample(example);
        return menuList;
    }

    @Override
    public void saveSortCode(Menu menu) {
        menuMapper.saveSortCode(menu);
    }

    @Override
    public List<Menu> queryChildrenByParentId(String pId) {
        MenuExample example
                = new MenuExample();
        example.createCriteria()
                .andIsValidEqualTo(1)
                .andParentIdEqualTo(pId);
        example.setOrderByClause("sort_code desc");
        return menuMapper.selectByExample(example);
    }

    @Override
    public List<Menu> selectMenuByIds(List<String> menuIdList) {
        if (CollectionUtils.isEmpty(menuIdList)) {
            return Lists.newArrayList();
        }
        MenuExample example = new MenuExample();
        example.createCriteria()
                .andIsValidEqualTo(1).
                andIdIn(menuIdList);
        List<Menu> menuList = menuMapper.selectByExample(example);
        return menuList;
    }
}
